package JAVA300.第九章容器;
/**
 * 自定义实现ArrayList 体会底层原理
 * 增加泛型
 */

public class TestMyArrayList2<E> {
    private Object[] elementData;
    private int size;
    private static final int DEFAULT_CAPACITY = 10;

    public TestMyArrayList2(){
        elementData = new Object[DEFAULT_CAPACITY];

    }

    public TestMyArrayList2(int capacity){
        elementData = new Object[capacity];

    }

    public void add(E element){
        elementData[size++] = element;
    }

    @Override
    public String toString() {

        StringBuilder stringBuilder = new StringBuilder();

        //[a,b]
        stringBuilder.append("[");
//        for (Object object:elementData){//[aa,bb,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,]
//            stringBuilder.append(object+",");
//        }
        for(int i=0;i<size;i++){
            stringBuilder.append(elementData[i]+",");//[aa,bb,]
        }
        stringBuilder.setCharAt(stringBuilder.length()-1, ']');
        //stringBuilder.append("]");
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        TestMyArrayList2 myArrayList = new TestMyArrayList2(20);
        myArrayList.add("aa");
        myArrayList.add("bb");

        System.out.println(myArrayList);
    }
}
